package com.gh.blog.service;

import com.gh.blog.dto.ArticleDto;
import com.gh.blog.entity.Article;
import com.gh.blog.entity.Category;
import com.gh.blog.mapper.ArticleMapper;
import com.gh.blog.mapper.CategoryMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Fawn
 * @Date 2020/9/10 9:44
 * @Version 1.0
 **/
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ArticleServiceImpl implements ArticleService {

    private final ArticleMapper mapper;

    private final CategoryMapper categoryMapper;

    @Override
    public PageInfo findAllArticleByPage(Integer current, Integer size) {
        PageHelper.startPage(current,size);
        List<Article> list=mapper.findAllArticleByPage();

        // 这里，要把Article的page对象转换为ArticleDTO的page对象
        // 所以，传入Article的page对象，也就是 new PageInfo(list)
        // 最后得到page
        PageInfo page = new ArticleDto().convertFrom(new PageInfo(list));

        List<ArticleDto> dtos = page.getList();
        for (ArticleDto articleDto : dtos) {
            Category category = categoryMapper.findById(articleDto.getCategory());
            articleDto.setCategoryObj(category);
        }
        return page;
    }
}
